<?php

namespace coser;

class Request
{
    //对象实例
    protected static $instance;

    //获取请求地址
    public static function path()
    {
        $parseUrl = $_SERVER['REQUEST_URI'];
		
        $path = str_replace('/index.php', '', $parseUrl);

        if ($path == '/' || $path == '') {
            $config = Config::get('config');
            $path = $config['default_module'] . '/' . $config['default_controller'] . '/' . $config['default_action'];
        }

        return $path;
    }

    //创建对象实例
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    //定义模块
    public function module($module = null)
    {
        if (!is_null($module)) {
            $this->module = $module;
            return $this;
        } else {
            return $this->module ?: '';
        }
    }

    //定义控制器
    public function controller($controller = null)
    {
        if (!is_null($controller)) {
            $this->controller = $controller;
            return $this;
        } else {
            return $this->controller ?: '';
        }
    }


    //定义操作
    public function action($action = '')
    {
        if(!is_null($action) && !is_bool($action)){
            $this->action = $action;
            return $this;
        }else{
            $name = $this->action ?: '';
            return true === $action ? $name : strtolower($name);
        }
    }

}